package com.czl.module_base.http;

/**
 * @author xubin
 * @date on 2018/9/18  15:54
 */
public class HttpResponse<T> {

    private static final long serialVersionUID = 5213230387175987835L;
    public static final int SUCCESS = 0;
    public static final String SUCCESS_CODE = "200";
    public static final int STATE_USER_NOT = 2;
    public static final int FAILED = 1;
    public static final int REFRESH = 999;

    private String msg;
    private boolean isNotConnected;
    private int state = -1;
    private int errorCode;
    private T data;
    private String error = "error";
    private String requestTag;
    private String code;

    public String getRequestTag() {
        return requestTag;
    }

    public void setRequestTag(String requestTag) {
        this.requestTag = requestTag;
    }

    public boolean isNotConnected() {
        return isNotConnected;
    }

    public void setNotConnected(boolean notConnected) {
        isNotConnected = notConnected;
    }

    public String getErrorMsg() {
        return msg == null ? "error" : msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return SUCCESS == state;
    }

    public boolean isSuccessCode() {
        return SUCCESS_CODE.equals(code);
    }

    public HttpResponse<T> createErrorHttpResponse() {
        this.setState(HttpResponse.FAILED);
        return this;
    }

    public HttpResponse<T> createHttpResponse(T t) {
        if (t == null) {
            this.setState(HttpResponse.FAILED);
        } else {
            this.setState(HttpResponse.SUCCESS);
        }
        setData(t);
        return this;
    }

    @Override
    public String toString() {
        return "HttpResponse{" +
                "msg='" + msg + '\'' +
                ", state=" + state +
                ", errorCode=" + errorCode +
                ", data=" + data +
                '}';
    }
}
